package net.lcheng.sellerCenter.controller;

import net.lcheng.commons.utils.PageList;
import net.lcheng.model.User;
import net.lcheng.model.WxShop;
import net.lcheng.model.WxShopUser;
import net.lcheng.service.UserService;
import net.lcheng.service.WxShopService;
import net.lcheng.service.WxShopUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * Created by zwr on 2016/7/5 0005.
 */
@Controller
public class WxShopUserContrller {
    @Autowired
    private WxShopUserService wxShopUsevService;
    @Autowired
    private UserService userService;
    @Autowired
    private WxShopService wxShopService;
    private final int PAGESIZE = 10;
    /**会员管理*/
   @RequestMapping(value = "wx/shop_user",method = RequestMethod.GET)
    public String memberManagementGet(Model model, @RequestParam(defaultValue = "1", required = false) int page){
    Subject currentUser = SecurityUtils.getSubject();
    String userPhoneNum = currentUser.getSession().getAttribute("UserPhoneNum").toString();
    //获取用户信息
    User user = userService.getUserByPhone(userPhoneNum);
    //获取店铺id
    WxShop shop = wxShopService.getShopInforByUserId(user.getUserId());
    PageList<WxShopUser> pageList=wxShopUsevService.getWxShopUsevByShopid(page,PAGESIZE,shop.getShopId());
    model.addAttribute("list", pageList.list);
    model.addAttribute("pageIndex", page);
    model.addAttribute("totalPage", pageList.totalPage);
     return "shop_user";
     }

    /***
     * 查找会员
     * @param model
     * @return zwr
     */
    @RequestMapping("/wx/lookup_user")
    public String studentSearch(Model model,@RequestParam(value = "userName",required = false)String userName){
        //获取当前用户
        Subject currentUser = SecurityUtils.getSubject();
        String userPhoneNum = currentUser.getSession().getAttribute("UserPhoneNum").toString();
        //获取用户信息
        User user = userService.getUserByPhone(userPhoneNum);
        //获取店铺id
        WxShop shop = wxShopService.getShopInforByUserId(user.getUserId());
        List<WxShopUser> wxShopUsers=wxShopUsevService.getWxShopUsevByUserName(userName,shop.getShopId());
        model.addAttribute("wxShopUsers",wxShopUsers);
        return "shop_user";
    }
 }
